
let { trackMoreFun, globalData } = getApp();
Component({
  data: {
    checkTextList: [
      { text: '清新', sku: ['P122120', 'P122150', 'P101560', 'P122080'], isChecked: false },
      { text: '振奋', sku: ['P122100', 'P122150', 'P101560', 'P122080'], isChecked: false },
      { text: '花香', sku: ['P122110', 'P122140', 'P122120', 'P122130', 'P122020', 'P122010', 'P122490'], isChecked: false },
      { text: '感性', sku: ['P122130', 'P122150', 'P122090', 'P122020', 'P122070', 'P122040', 'P122010', 'P122030', 'P122160', 'P122180', 'P122490'], isChecked: false },
      { text: '绵柔', sku: ['P122120', 'P122160', 'P122490'], isChecked: false },
      { text: '自然', sku: ['P101560', 'P122080', 'P122100'], isChecked: false },
      { text: '馥郁', sku: ['P122050', 'P122140', 'P122090', 'P122030', 'P122180', 'P122010'], isChecked: false },
      { text: '粉香', sku: ['P122140', 'P122060', 'P122160', 'P122020', 'P122070', 'P122490'], isChecked: false },
      { text: '神秘', sku: ['P122050', 'P122090', 'P122060', 'P122040', 'P122030', 'P122160', 'P122180'], isChecked: false },
      { text: '多面', sku: ['P122140', 'P122050', 'P122130', 'P122020', 'P122160', 'P122180', 'P122110', 'P122030', 'P122490'], isChecked: false },
      { text: '独特', sku: ['P122010', 'P122120', 'P122090', 'P122060', 'P122070', 'P122100', 'P122040'], isChecked: false },
    ],
    selectedItems: [],
    maxSelect: 3,
    area1Height: 0,
    scaleNum: 1
  },
  didMount() {
    globalData.pageNum = 2
    trackMoreFun('预览_发现你的嗅觉个性_选择嗅觉个性页面','into' )
    // globalData
    if (globalData.textData.selectedItems && globalData.textData.checkTextList) {
      const selectedItems = globalData.textData.selectedItems
      const checkTextList = globalData.textData.checkTextList
      this.setData({
        selectedItems,
        checkTextList
      })
    }
    const systemInfo = my.getSystemInfoSync();
    const bottomH = 418 //设计底部区域高度
    const areaH_ = 1030//设计稿文字区域高度
    // // 获取屏幕宽度和高度（单位：px）
    const screenWidth = systemInfo.screenWidth; // 屏幕宽度
    let screenHeight = systemInfo.screenHeight - systemInfo.statusBarHeight - systemInfo.titleBarHeight; // 屏幕高度
    // 将屏幕高度转换为 rpx
    let rem_ = screenWidth / 750
    let screen_h = screenHeight / rem_  //不同屏幕设备高度

    const content_h = screen_h - bottomH //不同设备 内容的高度

    let scaleNum = (content_h / areaH_).toFixed(2)
    scaleNum = (1 - scaleNum) <= 0.03 ? 1 : scaleNum
    if (scaleNum > 1) {
      scaleNum = 1
    }
    // 更新页面数据
    this.setData({
      area1Height: content_h,
      scaleNum: Number(scaleNum)
    });

  },
  methods: {
    onCheckText(e) {
      const index = e.currentTarget.dataset.index;
      const item = this.data.checkTextList[index];
      trackMoreFun(`点击_发现你的嗅觉个性_选择嗅觉个性_${item.text}`, 'click')
      let selectedItems = this.data.selectedItems;
      const isSelected = selectedItems.some(i => i.text === item.text);
      const updatedList = this.data.checkTextList.map((i, idx) => {
        if (idx === index) {
          return {
            ...i,
            isChecked: !isSelected
          };
        }
        return i;
      });

      let updatedSelected = isSelected
        ? selectedItems.filter(i => i.text !== item.text)
        : [...selectedItems, { ...item, isChecked: true }];

      if (!isSelected && updatedSelected.length > this.data.maxSelect) {
        return;
      }
      globalData.textData.selectedItems = updatedSelected
      globalData.textData.checkTextList = updatedList
      this.setData({
        selectedItems: updatedSelected,
        checkTextList: updatedList
      });
    },
    async changeNextTo() {
      try {
        if (this.data.selectedItems.length < 3) return;
        const textArr = []
        this.data.selectedItems.forEach(item => {
          textArr.push(item.text)
        })
        trackMoreFun(`点击_发现你的嗅觉个性_选择嗅觉个性_点击下一步_${textArr}`, 'click',)
        const list = await this.getStorage('selectArr');
        const list1 = [];
        this.data.selectedItems.forEach(i => {
          list1.push(i.sku);
        });
        list[1] = list1;
        this.props.onMyEvent({ num: 3 });

        my.setStorage({
          key: 'selectArr',
          data: list
        });
      } catch (error) {
        console.log('error', error);
      }
    },
    getStorage(key) {
      return new Promise((resolve, reject) => {
        my.getStorage({
          key,
          success: function (res) {
            resolve(res.data);
          },
          fail: function (res) {
            reject([]);
          }
        });
      });
    },
    changeBack() {
      this.props.onMyEvent({ num: 1 });
    },
  }
})
